package com.hsav.kcxb.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.utils.DensityUtil;
import com.hsav.kcxb.utils.LrcEntry;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiB on 2017/6/29.
 */

public class MusicClipView2 extends FrameLayout {

    private LinearLayout lrcContainer;
    private List<TextView> lrcViews;
    private List<LrcEntry> lrcEntries;

    public MusicClipView2(@NonNull Context context) {
        super(context);
        createContent();
    }

    public MusicClipView2(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        createContent();
    }

    private void createContent() {
        lrcContainer = new LinearLayout(getContext());
        lrcContainer.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                FrameLayout.LayoutParams params = (LayoutParams) lrcContainer.getLayoutParams();
                params.topMargin = Math.min(Math.max(params.topMargin, 0), getMeasuredHeight() - lrcContainer.getMeasuredHeight());
                lrcContainer.setLayoutParams(params);
                return true;
            }
        });
        lrcContainer.setOrientation(LinearLayout.VERTICAL);
        lrcContainer.setGravity(Gravity.CENTER_HORIZONTAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lrcContainer.setLayoutParams(params);
        addView(lrcContainer);

        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View handlerStart = layoutInflater.inflate(R.layout.content_music_clip_start_handler, this, false);
        TextView textStart = (TextView) handlerStart.findViewById(R.id.thumb);
        textStart.setText("开始");
        addView(handlerStart);

        View handlerEnd = layoutInflater.inflate(R.layout.content_music_clip_start_handler, this, false);
        TextView textEnd = (TextView) handlerStart.findViewById(R.id.thumb);
        textEnd.setText("结束");
        addView(handlerEnd);
    }

    public void loadLrc(final File lrcFile) {
        lrcEntries = LrcEntry.parseLrc(lrcFile);
        lrcViews = new ArrayList<>(lrcEntries.size());
        inflateLrc();
    }

    private void inflateLrc() {
        if (lrcEntries == null || lrcEntries.size() == 0) return;
        for (LrcEntry lrcEntry : lrcEntries)
        {
            TextView line = createLine(lrcEntry);
            lrcViews.add(line);
            lrcContainer.addView(line);
        }
    }

    private TextView createLine(LrcEntry lrcEntry) {
        TextView textView = new TextView(getContext());
        textView.setText(lrcEntry.getText());
        textView.setTextColor(getResources().getColor(R.color.color_white_normal));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int span = DensityUtil.dip2px(getContext(), 4);
        layoutParams.setMargins(0, span, 0, span);
        textView.setLayoutParams(layoutParams);
        return textView;
    }
}
